﻿using System.Collections.ObjectModel;
using System.Windows.Media;
using Windows_AI_Assistant.Models;
using Windows_AI_Assistant.Services;
using Wpf.Ui.Controls;

namespace Windows_AI_Assistant.ViewModels.Pages
{
    public partial class DataViewModel : ObservableObject, INavigationAware
    {
        private bool _isInitialized = false;

        [ObservableProperty]
        public DateTime _dateStart = DateTime.Today;

        [ObservableProperty]
        public DateTime _dateEnd = DateTime.Today;

        [ObservableProperty]
        public ObservableCollection<DialogContent>? _dialogContents = new();

        // 对话日志数据库服务
        private readonly DialogContentService _dialogContentService = new();

        public void OnNavigatedTo()
        {
            if (!_isInitialized)
                InitializeViewModel();
        }

        public void OnNavigatedFrom() { }

        private void InitializeViewModel()
        {
            // 加载对话内容
            RetrieveDialogs();
            // System.Windows.MessageBox.Show("DataViewModel initialized.");

            // 标记为已初始化
            _isInitialized = true;
        }

        [RelayCommand]
        public void SearchHistory()
        {
            RetrieveDialogs();
        }

        /// <summary>
        /// 检索对话日志
        /// </summary>
        private void RetrieveDialogs()
        {
            DialogContents.Clear();
            foreach (var dialog in _dialogContentService.GetAllDialogContents(_dateStart, _dateEnd))
            {
                DialogContents.Add(dialog);
            }
        }
    }
}
